package domain.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;
import java.util.regex.Pattern;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import domain.data.QnAPersistenceManager;
import domain.model.Domain;

public class CheckRobotSitemap {
	public CheckRobotSitemap(String name_domain)
	{
		PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();

		@SuppressWarnings("unchecked")
		List<Domain> domainList=  (List<Domain>)psm.newQuery(Domain.class,"name == '"+name_domain+"'").execute();
		if(domainList.size()>0)
		{
			Domain domain = domainList.get(0);
			String content = "";
			try {
				URL dataURL = new URL("http://"+name_domain+"/robots.txt");
				HttpURLConnection connection = (HttpURLConnection) dataURL.openConnection();
				connection.setReadTimeout(500000);
	    		connection.setConnectTimeout(1000000);
			    connection.setInstanceFollowRedirects(true);
			    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("utf-8")));
				while(1==1)
				{
					String str = reader.readLine();
					if(str==null) break;
					content+=str;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				URL dataURL = new URL("http://www."+name_domain+"/robots.txt");
				HttpURLConnection connection = (HttpURLConnection) dataURL.openConnection();
			    connection.setInstanceFollowRedirects(true);
			    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("utf-8")));
				while(1==1)
				{
					String str = reader.readLine();
					if(str==null) break;
					content+=str;
				}
			} catch (Exception e) {			
				e.printStackTrace();
			}
			
			if(Pattern.matches("^[\\W]*user-agent[\\w\\W]*", content.toLowerCase()))
			{
				domain.setRobot(1);
			}
			
			String contents = "";
			try {
				URL dataURL = new URL("http://"+name_domain+"/sitemap");
				HttpURLConnection connection = (HttpURLConnection) dataURL.openConnection();
			    connection.setInstanceFollowRedirects(true);
			    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("utf-8")));
				while(1==1)
				{
					String str = reader.readLine();
					if(str==null) break;
					contents+=str;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				URL dataURL = new URL("http://www."+name_domain+"/sitemap");
				HttpURLConnection connection = (HttpURLConnection) dataURL.openConnection();
			    connection.setInstanceFollowRedirects(true);
			    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("utf-8")));
				while(1==1)
				{
					String str = reader.readLine();
					if(str==null) break;
					contents+=str;
				}
			} catch (Exception e) {			
				e.printStackTrace();
			}
			Document doc = Jsoup.parse(contents);
			if(doc.select("urlset").size()>0)
			{
				domain.setSitemap(1);
			}
			psm=JDOHelper.getPersistenceManager(domain);
			psm.currentTransaction().begin();
			psm.makePersistent(domain);
			psm.currentTransaction().commit();
		}
	}
//	public static void main( String args[])
//	{
//		new CheckDomain();
//	}
}
